using UnityEngine;

public class PushObject : MonoBehaviour
{
	public float fallDistance = 50f;

	private float initialHeight;

	private Vector3 initialPosition;

	private Quaternion initialRotation;

	private bool dead;

	private Vector3 freezedVelocity;

	private Vector3 freezedAngularVelocity;

	private void Awake()
	{
		LevelController.LevelReadyEvent += OnLevelReady;
		EventManager.WorldResetEvent += OnWorldReset;
		EventManager.GameFreezeEvent += OnGameFreeze;
		EventManager.GameUnfreezeEvent += OnGameUnfreeze;
	}

	private void OnDestroy()
	{
		LevelController.LevelReadyEvent -= OnLevelReady;
		EventManager.WorldResetEvent -= OnWorldReset;
		EventManager.GameFreezeEvent -= OnGameFreeze;
		EventManager.GameUnfreezeEvent -= OnGameUnfreeze;
	}

	private void OnLevelReady(Transform levelTransform)
	{
		if (base.transform.root == levelTransform)
		{
			initialPosition = base.transform.position;
			initialRotation = base.transform.rotation;
			initialHeight = base.transform.position.y;
			base.GetComponent<Rigidbody>().isKinematic = false;
			dead = false;
		}
	}

	private void OnWorldReset()
	{
		base.transform.position = initialPosition;
		base.transform.rotation = initialRotation;
		base.GetComponent<Renderer>().enabled = true;
		base.GetComponent<Collider>().enabled = true;
		base.GetComponent<Rigidbody>().isKinematic = false;
		base.GetComponent<Rigidbody>().velocity = Vector3.zero;
		base.GetComponent<Rigidbody>().angularVelocity = Vector3.zero;
		dead = false;
	}

	private void OnGameFreeze()
	{
		if (!base.GetComponent<Rigidbody>().isKinematic)
		{
			freezedVelocity = base.GetComponent<Rigidbody>().velocity;
			freezedAngularVelocity = base.GetComponent<Rigidbody>().angularVelocity;
			base.GetComponent<Rigidbody>().velocity = Vector3.zero;
			base.GetComponent<Rigidbody>().angularVelocity = Vector3.zero;
			base.GetComponent<Rigidbody>().detectCollisions = false;
		}
	}

	private void OnGameUnfreeze()
	{
		if (!base.GetComponent<Rigidbody>().isKinematic)
		{
			base.GetComponent<Rigidbody>().WakeUp();
			base.GetComponent<Rigidbody>().velocity = freezedVelocity;
			base.GetComponent<Rigidbody>().angularVelocity = freezedAngularVelocity;
			base.GetComponent<Rigidbody>().detectCollisions = true;
		}
	}

	private void LateUpdate()
	{
		if (!dead && initialHeight - base.transform.position.y > fallDistance)
		{
			Die();
		}
	}

	private void FixedUpdate()
	{
		if (!(Time.fixedDeltaTime < 0.001f))
		{
		}
	}

	private void Die()
	{
		dead = true;
		base.GetComponent<Rigidbody>().velocity = Vector3.zero;
		base.GetComponent<Rigidbody>().angularVelocity = Vector3.zero;
		base.GetComponent<Rigidbody>().isKinematic = true;
		base.GetComponent<Renderer>().enabled = false;
		base.GetComponent<Collider>().enabled = false;
	}
}
